package com.shqcjd.generalplatformprojectclient.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.shqcjd.generalplatformprojectclient.R;
import com.shqcjd.generalplatformprojectclient.app.AppConstant;
import com.shqcjd.generalplatformprojectclient.model.ResponseData;
import com.shqcjd.generalplatformprojectclient.model.pojo.LoginBean;
import com.shqcjd.generalplatformprojectclient.model.pojo.UserEntity;
import com.shqcjd.generalplatformprojectclient.ui.base.BaseActivity;
import com.shqcjd.generalplatformprojectclient.ui.presenter.LoginRegistPresenter;
import com.shqcjd.generalplatformprojectclient.ui.view.LoginRegistView;
import com.shqcjd.generalplatformprojectclient.util.PrefUtils;
import com.shqcjd.generalplatformprojectclient.util.ToastUtils;
import com.shqcjd.generalplatformprojectclient.util.UIUtils;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

public class LoginActivity extends BaseActivity<LoginRegistView, LoginRegistPresenter> implements LoginRegistView{
    @BindView(R.id.et_login_name)
    EditText etLoginName;
    @BindView(R.id.et_login_password)
    EditText etLoginPassword;
    @BindView(R.id.btn_regist)
    Button btnRegist;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.iv_login_pic)
    ImageView ivLoginPic;
    @BindView(R.id.rb_login_phone)
    RadioButton rbLoginPhone;
    @BindView(R.id.rb_login_password)
    RadioButton rbLoginPassword;
    @BindView(R.id.tv_login_code)
    TextView tvLoginCode;
    @BindView(R.id.rg_login)
    RadioGroup rgLogin;
    @BindView(R.id.cb_login)
    CheckBox cbLogin;

    private Context context;
    private int loginType = 0;//0是验证码登陆，1是密码登录
    private String mCode = "-1";//验证码

    @Override
    protected LoginRegistPresenter createPresenter() {
        return new LoginRegistPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    public void init() {
        super.init();
        context = this;
    }

    @Override
    public void initView() {
        super.initView();
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .circleCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(this).load(R.mipmap.login_pic).apply(options).into(ivLoginPic);

        rbLoginPhone.setChecked(true);

        rgLogin.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                etLoginPassword.setText("");
                switch (checkedId){
                    case R.id.rb_login_phone:
                        loginType = 0;
                        tvLoginCode.setVisibility(View.VISIBLE);
                        etLoginPassword.setHint("输入短信验证码");
                        cbLogin.setVisibility(View.GONE);
                        cbLogin.setChecked(true);
                        break;
                    case R.id.rb_login_password:
                        loginType = 1;
                        tvLoginCode.setVisibility(View.GONE);
                        etLoginPassword.setHint("请输入登录密码");
                        cbLogin.setVisibility(View.VISIBLE);
                        cbLogin.setChecked(false);
                        break;
                }
                etLoginPassword.setText("");
            }
        });

        cbLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    etLoginPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    etLoginPassword.setSelection(etLoginPassword.getText().length());
                }else{
                    etLoginPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    etLoginPassword.setSelection(etLoginPassword.getText().length());
                }
            }
        });
    }


    @OnClick({R.id.btn_regist, R.id.btn_login, R.id.tv_login_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_regist:
                Intent it = new Intent(context, RegisterActivity.class);
                startActivity(it);
                break;
            case R.id.btn_login:
                if(UIUtils.isMobile(etLoginName.getText().toString())) {
                    if (loginType == 0) {
                        if (mCode.equals(etLoginPassword.getText().toString())) {
                            mPresenter.toCodeLogin(etLoginName.getText().toString(), etLoginPassword.getText().toString());
                        } else {
                            ToastUtils.showShort(context, "验证码错误");
                        }
                    } else {
                        mPresenter.toLogin(etLoginName.getText().toString(), etLoginPassword.getText().toString());
                    }
                }else{
                    ToastUtils.showShort(context, "请输入正确的手机号");
                }
                break;
            case R.id.tv_login_code:
                if(tvLoginCode.getText().toString().equals("获取验证码")){
                    String phone = etLoginName.getText().toString().trim();
                    if(UIUtils.isMobile(phone)){
                        tvLoginCode.setTextColor(Color.GRAY);
                        mPresenter.getCode(phone);
                    }else{
                        ToastUtils.showShort(context, "请输入正确的手机号");
                    }
                }
                break;
        }
    }


    @Override
    public void showProgress(String tipString) {
        showWaitingDialog(tipString);
    }

    @Override
    public void hideProgress() {
        hideWaitingDialog();
    }

    @Override
    public void loginSuccess(ResponseData<LoginBean> loginBean) {
        if(loginBean.code==0){
            ToastUtils.showShort(context, "登录成功");
            PrefUtils.setString(context, PrefUtils.COMPANY_NAME, loginBean.data.userVO.cname);
            PrefUtils.setInt(context, PrefUtils.COMPANY_ID, loginBean.data.userVO.cid);
            PrefUtils.setString(context, PrefUtils.USER_PHONE, loginBean.data.userVO.phoneNumber);
            PrefUtils.setInt(context, PrefUtils.USER_ROLE_ID, loginBean.data.userVO.roleId);
            PrefUtils.setInt(context, PrefUtils.USER_USER_ID, loginBean.data.userVO.id);
//            PrefUtils.setInt(context, PrefUtils.USER_USER_ID, 28);//todo 测试用id 正式请删除
            PrefUtils.setString(context, AppConstant.TOKEN_KEY, loginBean.data.token);
            Intent it = new Intent(context, MainActivity.class);
            startActivity(it);
            finish();
        }else{
            ToastUtils.showShort(context, loginBean.message);
        }
    }

    @Override
    public void registerSuccess(UserEntity userEntity) {

    }

    @Override
    public void loginFail() {
        Toasty.warning(this,"登录失败").show();
    }

    @Override
    public void registerFail() {

    }

    @Override
    public void getCodeTime(int time) {
        tvLoginCode.setText("还剩"+time+"秒");
    }

    @Override
    public void getCodeEnd() {
        tvLoginCode.setTextColor(Color.BLACK);
        tvLoginCode.setText("获取验证码");
    }

    @Override
    public void codeNum(String code) {
        mCode = code;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPresenter!=null){
            mPresenter.clear();
        }
    }
}
